package com.web.controller;


import com.web.entity.DietRecord;
import com.web.entity.HealthInfo;
import com.web.entity.Result;
import com.web.service.DietRecordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/dietRecord")
@Slf4j
@CrossOrigin
public class DietRecordController {

    @Autowired
    private DietRecordService dietRecordService;


    //http://localhost:8080/dietRecord/getByUserIdAndDate?userId=2&recordDate=2025-09-13

    @GetMapping("/getByUserIdAndDate")
    public Result getByUserIdAndDate(DietRecord dietRecord){
        log.info("查询结果：{}", dietRecord);
        List<DietRecord> byUserIdAndDate = dietRecordService.getByUserIdAndDate(dietRecord);
        return Result.success(byUserIdAndDate);
    }




    //增加饮食记录http://localhost:8080/dr/add
    /*{
    "userId": 1001,
    "doctorId": 2,
    "foodName": "酸奶+坚果",
    "calorie": 180.40,
    "protein": 8.60,
    "carbohydrate": 15.20,
    "fat": 9.80,
    "mealTime": "加餐",
    "recordDate": "2025-09-13",
    "description": "下午加餐，补充能量"
    }*/
    @PostMapping("/add")
    public Result add(@RequestBody DietRecord dietRecord){
        log.info("新增饮食记录{}",dietRecord);
        dietRecordService.add(dietRecord);
        return Result.success("新增饮食记录成功");
    }

    //根据id删除饮食记录http://localhost:8080/dr/delete/4
    @DeleteMapping("/delete/{id}")
    public Result deleteById(@PathVariable int id){
        log.info("根据id删除饮食记录{}",id);
        dietRecordService.deleteById(id);
        return Result.success("根据id删除饮食记录成功");
    }

    //根据id修改饮食记录http://localhost:8080/dr/update/5
    /*{
    "userId": 1001,
    "doctorId": 2,
    "foodName": "猪大肠",
    "calorie": 180.40,
    "protein": 8.60,
    "carbohydrate": 15.20,
    "fat": 9.80,
    "mealTime": "加餐",
    "recordDate": "2025-09-13",
    "description": "下午加餐，补充能量"
    }*/
    @PutMapping("/update/{id}")
    public Result update(@PathVariable int id,@RequestBody DietRecord dietRecord){
        log.info("根据id修改饮食记录{}",dietRecord);
        dietRecordService.update(id,dietRecord);
        return Result.success("根据id修改饮食记录成功");
    }

    //根据id查询饮食记录http://localhost:8080/dr/findById/5
    @GetMapping("/findById/{id}")
    public Result findById(@PathVariable int id){
        log.info("根据id查询饮食记录{}",id);
        DietRecord dietRecord = dietRecordService.findById(id);
        return Result.success(dietRecord);
    }

    //查询所有饮食记录http://localhost:8080/dr/findAll
    @GetMapping("/findAll")
    public Result findAll(){
        log.info("查询所有饮食记录");
        List<DietRecord> dietRecordList = dietRecordService.findAll();
        return Result.success(dietRecordList);
    }

}